Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Conversation

@stnma7e
Copy link
Contributor

@stnma7e stnma7e commented Jul 12, 2016

Allows a the destructuring of a Json type with move semantics.

For example,

let json_value = Json::from_str("{}").unwrap();
let json_object = (|json: Json| {
    // doesn't compile
    json.as_object()
})(json_value);
assert!(json_object.is_some());

if a Json type is passed to a closure, or some other scope block, when trying to destructure it, there is a lifetime conflict. The Json given as a parameter to the closure was moved, but it cannot be moved out of the closure as an Object.

If you replace as_object with into_object, the Object value returned is not a reference, so it can be moved out of the scope.

But this works,

let json_value = Json::from_str("{}").unwrap();
let json_object = (|json: Json| {
    json.into_object()
})(json_value);
assert!(json_object.is_some());

Allows a the destructuring of a Json type with move semantics.

For example,

    let json_value = Json::from_str("{}").unwrap();
    let json_object = (|json: Json| {
        json.as_object()
    })(json_value);
    assert!(json_object.is_some());

if a Json type is passed to a closure, or some other scope block, when
trying to destructure it, there is a lifetime conflict. The Json given
as a parameter to the closure was moved, but it  cannot be moved out of
the closure as an Object. If you replace as_object with into_object, the
Object value returned is not a reference, so it can be moved out of the scope.
@alexcrichton alexcrichton merged commit 08503bd into rust-lang-deprecated:master Jul 12, 2016
@alexcrichton
Copy link
Contributor

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants